package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.starseas_phone.Entity.College;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddClassActivity extends AppCompatActivity {

    private TextView add_class_Classid;
    private Spinner add_class_CollegeId;
    private Spinner add_class_ProfessionalId;
    private TextView add_class_Grade;
    private Spinner add_class_WorkerId;
    private Button addstu_stu_btn;

    private List<String> Collegelist =new ArrayList<String>();
    private List<Integer> CollegeIdlist =new ArrayList<Integer>();
    private List<String> Professionallist =new ArrayList<String>();
    private List<Integer> ProfessionalIdlist =new ArrayList<Integer>();
    private List<String> Workerlist =new ArrayList<String>();
    private List<String> WorkerIdlist =new ArrayList<String>();
    private int selectCollegeid;
    private int selectprofessionalId;
    private String selectWorkerId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_class);
        initid();
        initcolleglist();

        addstu_stu_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tijiao();
            }
        });
        add_class_CollegeId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                selectCollegeid = Collegelist.get(i);
//                add_class_CollegeId.setSelection(i,true);
                selectCollegeid = CollegeIdlist.get(i);
                Professionallist.clear();
                ProfessionalIdlist.clear();
                findzy(selectCollegeid);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        add_class_ProfessionalId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectprofessionalId = ProfessionalIdlist.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        add_class_WorkerId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectWorkerId = WorkerIdlist.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        findbzr();
    }

    private void initid(){
        add_class_Classid = (TextView) findViewById(R.id.add_class_Classid);
        add_class_CollegeId = (Spinner) findViewById(R.id.add_class_CollegeId);
        add_class_ProfessionalId = (Spinner) findViewById(R.id.add_class_ProfessionalId);
        add_class_Grade = (TextView) findViewById(R.id.add_class_Grade);
        add_class_WorkerId = (Spinner) findViewById(R.id.add_class_WorkerId);
        addstu_stu_btn = (Button) findViewById(R.id.addstu_stu_btn);
    }

    private void initcolleglist(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/College/GetAllCollege")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        String CollegeName = jsonObjects.getString("CollegeName");
                        int CollegeId = jsonObjects.getInt("CollegeId");
                        CollegeIdlist.add(CollegeId);
                        Collegelist.add(CollegeName);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddClassActivity.this,R.layout.classid_item, R.id.text,Collegelist);
                            add_class_CollegeId.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void findzy(int id){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Professional/GetProfessionalsByCollegeId?CollegeId="+id)
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        String ProfessionalName = jsonObjects.getString("ProfessionalName");
                        int ProfessionalId = jsonObjects.getInt("ProfessionalId");
                        ProfessionalIdlist.add(ProfessionalId);
                        Professionallist.add(ProfessionalName);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddClassActivity.this,R.layout.classid_item, R.id.text,Professionallist);
                            add_class_ProfessionalId.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void findbzr(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Worker/GetAllWorker")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        String WorkerName = jsonObjects.getString("WorkerName");
                        String WorkerId = jsonObjects.getString("WorkerId");
                        WorkerIdlist.add(WorkerId);
                        Workerlist.add(WorkerName);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddClassActivity.this,R.layout.classid_item, R.id.text,Workerlist);
                            add_class_WorkerId.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void tijiao(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("id",1);
                    jsonObject1.put("classId",add_class_Classid.getText().toString());
                    jsonObject1.put("collegeId",selectCollegeid);
                    jsonObject1.put("professionalId",selectprofessionalId);
                    jsonObject1.put("grade",add_class_Grade.getText().toString());
                    jsonObject1.put("workerId",selectWorkerId);

                    MediaType mediaType = MediaType.parse("application/json-patch+json");
                    RequestBody requestBody = RequestBody.create(mediaType,jsonObject1.toString()+"");
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Class/AddClasses?token="+Sto.dept.getToken())
                            .post(requestBody)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    Log.d("responseString",responseString);
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (msg.equals("操作成功")){
                                Toast.makeText(AddClassActivity.this,msg,Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(AddClassActivity.this,msg,Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}